package util;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.utils.PdfMerger;

import java.io.File;
import java.util.List;

/**
 * @author 别人
 */
public class PdfUtils {

    /**
     * 合并pdf文件
     */
    public static File mergeFiles(List<File> files, String fileName) throws Exception {
        File outFile = new File(fileName);

        PdfWriter writer = new PdfWriter(outFile);
        try (PdfDocument pdfDocument = new PdfDocument(writer)) {
            PdfMerger merger = new PdfMerger(pdfDocument);
            merger.setCloseSourceDocuments(true);

            for (File file : files) {
                PdfReader reader = new PdfReader(file);
                PdfDocument inDocument = new PdfDocument(reader);
                merger.merge(inDocument, 1, inDocument.getNumberOfPages());
            }
            merger.close();
        }
        return outFile;
    }

}